<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
    <title>美策平台</title>
    <link rel="icon" type="image/png" href="/img/icon.png">
    <link rel="stylesheet" type="text/css" href="../../datepicker/jquery-ui.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/app.css">
    <link rel="stylesheet" type="text/css" href="../../css/index.css">
    <link rel="stylesheet" type="text/css" href="../../css/all.css">
    <script type="text/javascript" src="../../js/jquery.min.js"></script>
    <script type="text/javascript" src="/h5/js/all.js"></script>
    <style>
        #addJob2Tlb { padding:10px 20%;
    background:#0088cc;border-radius:5px;
    border:0;color:#ffffff; }
    </style>
</head>
<body>
<!--
    制作者：九哥
    页面：发布岗位表单页面
    时间：2019-03-01
-->


<style type="text/css">
    .detail::before{
        content: '*';
        display: inline-block;
        color: #de373e;
    }
    .submit button{
        width: 80%;
        padding: 10px 20%;
        background: #0088cc;
        border-radius: 5px;
        border: 0;
        color: #ffffff;
    }
</style>


<!----------发布岗位页面---------->
<div id="addJob2Page"><!--此ID页面切换用-->
    <div id="addJob2Title">
        <b>发布岗位</b>
        <img src="../../images/return.png" id="addJob2Rtn">
    </div>
    <div id="addJob2Detail">
        <p class="title">职位：<b id="job_name"></b></p>
        <div class="detail">联系人
            <span><input name="contact_name" placeholder="请输入" type="text"></span>
        </div>

        <div class="detail">技术工种
            <span>
                <input type="text" name="skill" placeholder="请输入">
            </span>
        </div>

        <div class="detail">性别要求
            <ul>
                <li><input name="sex" type="radio" value="1" style="width:15px;height:15px;">男</li>
                <li><input name="sex" type="radio" value="2" style="width:15px;height:15px;">女</li>
                <li><input name="sex" type="radio" value="3" style="width:15px;height:15px;">不限</li>
            </ul>
        </div>

        <div class="detail">联系电话
            <span><input name="contact_telephone" placeholder="请输入" type="text"></span>
        </div>
        <div class="detail">工作地点
            <span><input name="address" placeholder="请输入" type="text"></span>
        </div>
        <div class="detail">福利待遇
            <ul id="welfare">
                <!--<li><input name="welfare" type="radio" value="0">暂无</li>-->
                <!--<li><input name="welfare" type="radio" value="1">包住</li>-->
                <!--<li><input name="welfare" type="radio" value="2">包吃</li>-->
            </ul>
            <br><br><br>
        </div>
        <div class="detail">人数
            <span><input name="num" type="number" placeholder="请输入" pattern="\d+"></span>
            <br>
        </div>

        <div class="detail">
            <p class="titlespan">岗位要求</p>
            <textarea class="textarea" name="detail" placeholder="请输入" ></textarea>
        </div>

        <div class="detail">
            <p class="titlespan">公司信息</p>
            <textarea class="textarea" name="content" placeholder="请输入"  ></textarea>
        </div>



        <div class="detail">
            <p class="titlespan">相册</p>
            <div class="productPictureDiv buttondiv">
                <table class="productPicture pictureDivTable">


                </table>
                <a class="addPic button">添加照片</a>
            </div>
        </div>
















        <div class="detail">月薪
            <span><input name="emolument" type="text" placeholder="比如:8K-10K" pattern="\d+"></span>
            <br>
        </div>
        <div class="detail">公司名称
            <select name="company_name" class="company_name">
            </select>
            <br>
        </div>

        <div class="detail">工作时间
            <span><input name="work_hour" placeholder="如：09:00-18:00" pattern="\d\d:\d\d" type="text" value="09:00-18:00"></span>
        </div>

        <div class="detail">休班制度
            <ul id="rest">
                <!--<li><input name="rest" type="radio" value="1">单休</li>-->
                <!--<li><input name="rest" type="radio" value="2">双休</li>-->
            </ul>
            <br>
        </div>

        <div class="detail">学历
            <ul id="edu">
                <!--<li><input name="education" type="radio" value=1>小学</li>-->
                <!--<li><input name="education" type="radio" value=2>初中</li>-->
                <!--<li><input name="education" type="radio" value=3>高中</li>-->
                <!--<li><input name="education" type="radio" value=4>大专</li>-->
                <!--<li><input name="education" type="radio" value=5>本科</li>-->
                <!--<li><input name="education" type="radio" value=7>其他</li>-->
            </ul>
            <br><br><br><br><br><br>
        </div>
        <div class="detail">工作年限
            <ul id="workYear">
                <!--<li><input name ="work_year" type="radio" value=1>1年以内</li>-->
                <!--<li><input name ="work_year" type="radio" value=2>1-2年</li>-->
                <!--<li><input name ="work_year" type="radio" value=3>3-5年</li>-->
                <!--<li><input name ="work_year" type="radio" value=4>5年以上</li>-->
            </ul>
            <br><br><br>
        </div>

        <div class="detail">年龄要求
            <input type="number" name="age_range1"
                   placeholder="输入数字" pattern="\d\d" style="width:30%;"> ~ <input type="number" placeholder="输入数字" pattern="\d\d" name="age_range2" style="width:30%;">
        </div>





        <div class="detail">邮箱
            <span><input name="email" placeholder="qq@qq.com" type="text" value=""></span>
        </div>
        <div class="detail">推广：暂时免费</div>
        <!-- <p class="submit">
            <button type="button" id="addJob2" onclick="test();return false;">发布</button>
        </p> -->
        <p class="submit">
            <button type="button" id="addJob2Tlb" data-status="0">发布</button>
        </p>
    </div>
</div>
<!----------发布岗位页面---------->





<!----------错误提示---------->
<div id="errMsg"></div>
<!----------错误提示---------->
</body>
<script type="text/javascript" src="../../js/indexPageChange.js"></script>
<script type="text/javascript" src="../../js/index.js"></script>
<script type="text/javascript" src="../../js/alertMsg.js"></script>
<script type="text/javascript">
    document.getElementById('job_name').innerHTML = localStorage.getItem('addjob_job_name');
    $("#addJob2Rtn").click(function(){
        window.location.href = "../addJob.html";
    });

    $("#addJob2Tlb").click(function(){
        if ($(this).attr('data-status') != 0) { // 去除多次点击
            return false
         }
         var mthis=this;
        $(this).attr('data-status', 1);
        setTimeout(function(){
            $(mthis).attr('data-status',0);
        },1000);
        var token = JSON.parse(localStorage.getItem('userInfo')).token;
        var jobId = localStorage.getItem('addjob_job_id');                  // 职位id
        var num = $("input[name='num']").val();                             // 招聘人数
        var emolument = $('input[name = emolument]').val();                 // 薪酬
        var skill = $('input[name = skill]').val();                         // 技术工种
        var rest = $('input[name = rest]:checked').val();                     // 休息制度
        var welfare = $("input[name='welfare']:checked").val();               // 福利待遇
        var education = $('input[name = education]:checked').val();           // 学历
        var work_year = $('input[name = work_year]:checked').val();           // 工作年限
        var sex = $('input[name = sex]:checked').val();                       // 性别
        var age_range1 = $("input[name='age_range1']").val();               // 年龄要求开始
        var age_range2 = $("input[name='age_range2']").val();               // 年龄要求结束
        var address = $("input[name='address']").val();                     // 工作地点
        var detail = $("[name='detail']").val();                       // 职位描述
        var content = $("[name='content']").val();                     // 工作内容
        var work_hour = $("input[name='work_hour']").val();                 // 工作时间
        var contact_name = $("input[name='contact_name']").val();           // 联系人
        var contact_telephone = $("input[name='contact_telephone']").val(); // 联系电话
        var email = $("input[name='email']").val();
        var name = {
            token: {name: 'token', value: token },
            job: {name: '职位id', value: jobId },
            num: {name: '招聘人数', value: num },
            emolument: {name: '薪酬', value: emolument },
            skill: {name: '技术工种', value: skill},
            rest: {name: '休息制度', value: rest },
            welfare: {name: '福利待遇', value: welfare },
            education: {name: '学历', value: education },
            work_year: {name: '工作年限', value: work_year },
            sex: {name: '性别', value: sex },
            age_range: {name: '年龄要求', value: age_range1 + ',' + age_range2, value1: age_range1, value2: age_range2 },
            address: {name: '工作地点', value: address },
            detail: {name: '职位描述', value: detail },
            content: {name: '工作内容', value: content },
            work_hour: {name: '工作时间', value: work_hour },
            contact_name: {name: '联系人', value: contact_name },
            contact_telephone: {name: '联系电话', value: contact_telephone },
            email: {name: '邮箱', value: email },
        }
        var data = {};
        try {
            Object.keys(name).forEach(function(k){
                if (k === 'age_range') {
                    if (name[k]['value1'] === '' || name[k]['value2'] === '') {
                        throw new Error(name[k]['name'] + '不能为空')
                    }
                }
                if (k === 'email') {
                    var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
                    if (!reg.test(email)) {
                        throw new Error(name[k]['name'] + '格式错误')
                    }
                }
                if (!name[k]['value'] || name[k]['value'] === '' || name[k]['value'] === undefined) {
                    throw new Error(name[k]['name'] + '不能为空')
                }

                data[k] = name[k]['value']
            });
        } catch (e) {
            $(mthis).attr('data-status',0);
            alertMsg(e.message)
            return
        }
        //格式验证
        if (!/^[0-9]{1,2}$/.test(age_range1)) {
            alertMsg('年龄要求1格式错误!'); return
        }
        if (!/^[0-9]{1,2}$/.test(age_range2)) {
            alertMsg('年龄要求2格式错误！'); return
        }
        if (!/^[0-9]{2}:[0-9]{2}[-~ ][0-9]{2}:[0-9]{2}$/.test(work_hour)) {
            alertMsg('工作时间格式错误！'); return
        }
        if (!/^1[3456789]\d{9}$/.test(contact_telephone)) {
            alertMsg('联系电话格式错误！'); return
        }
        var url = '/api/home/release/recruit';
//        var formData = JSON.stringify(data);
        data.company_name=$('.company_name').val();
        data.app_access=app_access;
        data.imgDetails=getImgDetails();
        var formData = data;
        $.ajax({
            url: url,
            type:'post',
            data:formData,
            success: function (res) {
                //console.log(res); return
                if (res.code!=0) {
                    alertMsg(res.msg);return;
                }
                alertMsg('发布成功');
                setTimeout(function() {
                    window.location.href = '/h5/my/job/zhaopin.html';
                }, 1000);
            }
        });
    });
    /**
     * 获取选项数据
     * */
    (function(){
        getOptionList();
    })();
    function getOptionList(){
        var userInfo = cacheGet('userInfo');
        var formData = {
            app_access:app_access//,
         //   token:userInfo.token
        };
        $.ajax({
            type:'post',
            url:getDomain()+'/option',
            data:formData,
            success: function (res) {
                if (res.code!=0) {
                    MC.msg.alert(res.msg,function(){
                        MC.back();
                    });

                    return;
                }
                console.log(res);
                var welfares = res.data.welfares.split(","); //福利待遇
                var rests = res.data.rests; //休息制度
                var edus = res.data.edus; //学历
                var workYears = res.data.workYears; //工作年限
                var h = '', h2 = '', h3 = '', h4 = '';
                for (var i in welfares) {
                    h += '<li><label>';
                    h += '<input type="radio" name="welfare" value="'+i+'" style="width:15px;height:15px;"> ';
                    h += welfares[i];
                    h += '</label></li>';
                }
                for (var i2 in rests) {
                    h2 += '<li><label>';
                    h2 += '<input type="radio" name="rest" value="'+i2+'" style="width:15px;height:15px;"> ';
                    h2 += rests[i2];
                    h2 += '</label></li>';
                }
                for (var i3 in edus) {
                    h3 += '<li><label>';
                    h3 += '<input type="radio" name="education" value="'+i3+'" style="width:15px;height:15px;"> ';
                    h3 += edus[i3];
                    h3 += '</label></li>';
                }
                for (var i4 in workYears) {
                    h4 += '<li><label>';
                    h4 += '<input type="radio" name="work_year" value="'+i4+'" style="width:15px;height:15px;"> ';
                    h4 += workYears[i4];
                    h4 += '</label></li>';
                }
                $('#welfare').html(h);
                $('#rest').html(h2);
                $('#edu').html(h3);
                $('#workYear').html(h4);
                for(var i=0;i<res.data.company_name.length;i++){
                    var str=res.data.company_name[i];
                    var li='<option value ="'+str+'">'+str+'</option>';
                    $('.company_name').append(li);
                }

            }
        });
    }
    function getDomain(){
        return '/api/home/release';
    }
    function addPictureDiv (div, name, src) {
        var div=$(div);
        var html = $("<tr class=\"trPic\">\n                <td class=\"name\">" + name + "</td>\n                <td class=\"right upImg imge\">\n                    <div class=\"file\" style=\"background-image: url(" + src + ");\">\n                        <input type=\"hidden\"  name=\"picsrc\" value=\"" + src + "\"/>\n\t\t\t\t\t\t<input type=\"file\" name=\"img\" />\n                    </div>\n                    <a class=\"deleteico\"></a>\n                </td>\n\t\t\t</tr>");
        $(div).append(html);
        html.find('.deleteico').on('click', function () {
            html.remove();
        });
        MC.API.bindFileUp(html.find('[type=file]'), function (src) {
            html.find('[name=picsrc]').val(src);
            html.find('.file').css('background-image', 'url(' + src + ')');
        });
        $('.productPictureDiv .NullPic').hide();
    }

    $('.productPictureDiv .addPic').click(function(){
        MC.msg.Pic(function(json){
            addPictureDiv('.pictureDivTable', json.name, json.src);
        }, '工作环境,工位');
    });


    function getImgDetails() {
        var list = $('.pictureDivTable .trPic');
        var listImg = [];
        for (var i = 0; i < list.length; i++) {
            var li = $(list[i]);
            var one = {
                name: li.find('.name').text(),
                src: li.find('[name=picsrc]').val()
            };
            listImg.push(one);
        }
        return  JSON.stringify(listImg);
    };

    //addPictureDiv('测试','http://www.fuzhuangchangongxiao.cn/uploads/20191106/5656feedf710a6056073d9968e3f0fa3small.jpg');

</script>
</html>